# -*- coding: utf-8 -*-
import hashlib
from model import Collection, db


class Student(Collection):
    '''
* `_id` (ObjectId) - ID of each student
* `askList` ([string]) - ask list of user, updated when student post a problem
* `avatarUrl` (string) - Url of avatar
* `collection` ([string]) - collection of user
* `commentList` ([string]) - comment list of user, updated when student post a comment
* `email` (string) - email of user
* `fan` ([string]) - fan of user
* `follow` ([string]) - people followed by user
* `gender` (int) - 0 means female, 1 means male, 2 means undetermined
* `name` (string) - user name
* `password` (string) - password of user
* `phone` (int) - phone of user
* `school` (string) - school of user
* `subscription` ([string]) - subscription of user
* `workList` ([string]) - work list of user, update when student post their works
* `place` (dict) - place of user
    * `province` (string) - province of user
    * `city` (string) - city of user
* `register` (int) - 1 if user is verified
* `wechat` (dict) - user info from wechat
    * `pId` (string) - ID of wechat
    * `pToken` (string) - Token of wechat
* `weibo` (dict) - user info from weibo
    * `pId` (string) - ID of weibo
    * `pToken` (string) - Token of weibo
    '''

    collection = db.student

    class Field(object):
        _id = '_id'
        askList = 'askList'
        avatarUrl = 'avatarUrl'
        collection = 'collection'
        commentList = 'commentList'
        email = 'email'
        fan = 'fan'
        follow = 'follow'
        gender = 'gender'
        name = 'name'
        password = 'password'
        phone = 'phone'
        school = 'school'
        subscription = 'subscription'
        workList = 'workList'
        place = 'place'
        register = 'register'
        weibo = 'weibo'

        class Weibo(object):
            pId = 'weibo.pId'
            pToken = 'weibo.pToken'

        class Wechat(object):
            pId = 'wechat.pId'
            pToken = 'wechat.pToken'

    class Gender(object):
        female = 0
        male = 1
        other = 2

    class Place(object):
        city = 'city'
        province = 'province'

    class Register(object):
        unverified = 0
        verified = 1

    class Weibo(object):
        pId = 'pId'
        pToken = 'pToken'

    class Rank(object):
        pass

    class Wechat(object):
        pId = 'pId'
        pToken = 'pToken'

    collection.ensure_index(Field.email, sparse=True, unique=True)
    collection.ensure_index(Field.phone, sparse=True, unique=True)
    collection.ensure_index(Field.Weibo.pId, sparse=True, unique=True)
    collection.ensure_index(Field.Wechat.pId, sparse=True, unique=True)

    @staticmethod
    def new_student(email, password):
        md5 = hashlib.md5()
        md5.update(password)
        hash_password = md5.hexdigest()
        return Student.insert({
            Student.Field.askList: [],
            Student.Field.avatarUrl: '',
            Student.Field.collection: [],
            Student.Field.commentList: [],
            Student.Field.email: email,
            Student.Field.fan: [],
            Student.Field.follow: [],
            Student.Field.gender: Student.Gender.other,
            Student.Field.name: '',
            Student.Field.password: hash_password,
            Student.Field.school: '',
            Student.Field.subscription: [],
            Student.Field.workList: [],
            Student.Field.place: {
                Student.Place.city: '',
                Student.Place.province: '',
            },
            Student.Field.register: Student.Register.verified,
        })

    @staticmethod
    def new_student_from_weibo(uid, name, access_token):
        return Student.insert({
            Student.Field.askList: [],
            Student.Field.avatarUrl: '',
            Student.Field.collection: [],
            Student.Field.commentList: [],
            Student.Field.fan: [],
            Student.Field.follow: [],
            Student.Field.gender: Student.Gender.other,
            Student.Field.name: name,
            Student.Field.password: '',
            Student.Field.school: '',
            Student.Field.subscription: [],
            Student.Field.workList: [],
            Student.Field.place: {
                Student.Place.city: '',
                Student.Place.province: '',
            },
            Student.Field.register: Student.Register.verified,
            Student.Field.weibo: {
                Student.Weibo.pId: uid,
                Student.Weibo.pToken: access_token,
            }
        })
